# -*- coding:utf-8 -*-

"""
    删除一个资源zip包
    * 删除之后的数据会保存到delete表里
"""

import tornado.web
from service import base
from configure import config
from utils.routes import route
from utils import log as logger
from utils import mongo



@route('/manage/delete/?', 'manage.delete')
class DeleteHandler(base.BaseWebHandler):
    """ 删除zip资源数据
    """
    db_name = config.DB_NAME
    collection_name = config.COLLECTION_DATAS
    collection_delete = config.COLLECTION_DELETE

    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self, *argl, **argd):
        self.post(*argl, **argd)

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self, *argl, **argd):
        _id = self.get_argument('_id', None)
        logger.debug('_id =', _id, caller=self)

        if not _id:
            self.finish({'info': '参数错误！', 'code': 1})
            return

        spec = {'_id': _id}
        datas = yield tornado.gen.Task(mongo.find_one, self.db_name, self.collection_name, spec)
        if not datas:
            self.finish({'info': '删除失败！', 'code': 2})
            return

        # 删除datas库里的记录，然后把被删除之后的数据存放到delete库里
        yield tornado.gen.Task(mongo.remove, self.db_name, self.collection_name, spec)
        del datas['_id']
        yield tornado.gen.Task(mongo.insert, self.db_name, self.collection_delete, datas)
        self.finish({'info': '删除成功！', 'code': 0})

